/****** Object:  Stored Procedure dbo.CGO_TriviaSendAnswer    Script Date: Monday, March 07, 2011 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'dbo.CGO_TriviaSendAnswer') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure dbo.CGO_TriviaSendAnswer
GO

/* -------------------------------------------------------------------------------------
/   AccountGet
/  ------------------------------------------------------------------------------------- */
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE dbo.CGO_TriviaSendAnswer	
  @EnrollID INT,
  @UserID INT,
  @QuestionIndex INT,
  @UserAnswerID INT,
  @Duration DECIMAL (10,3),
  @UserScore INT
AS
  IF NOT EXISTS ( SELECT  1
                  FROM    dbo.CGO_GameEnrollment M
                          INNER JOIN dbo.CGO_GameEnrollmentStatus S ON M.EnrollStatusID = S.StatusID
                  WHERE   EnrollmentID = @EnrollID
                          AND UserID = @UserID
                          AND StatusCode = 'PLAYING' )
    OR NOT EXISTS ( SELECT  1
                    FROM    dbo.CGO_TriviaResult
                    WHERE   GameEnrollmentID = @EnrollID
                            AND QuestionIndex = @QuestionIndex
                            AND ResultStatusID = 1 ) 
  BEGIN
    RETURN
  END
    
  DECLARE @CorrectAnswerID INT
  DECLARE @Correct BIT
  DECLARE @SCORE_PER_QUESTION DECIMAL
  DECLARE @Total_Score INT
  
  SELECT  @SCORE_PER_QUESTION = CONVERT(DECIMAL, ConfigValue)
  FROM    dbo.CGO_Configuration
  WHERE   ConfigCode = 'TRIVIA_SCORE_PER_QUESTION'
  
  -- If cheat the game, no socre at all
  IF @UserScore > @SCORE_PER_QUESTION
  BEGIN
  	SET @UserScore = 0
  END
      
  SELECT  @CorrectAnswerID = Q.CorrectAnswerID
  FROM    dbo.CGO_TriviaResult M
          INNER JOIN dbo.CGO_TriviaQuestion Q ON M.QuestionID = Q.QuestionID
  WHERE   M.GameEnrollmentID = @EnrollID
          AND QuestionIndex = @QuestionIndex
          
  IF @UserAnswerID IS NOT NULL AND @CorrectAnswerID=@UserAnswerID
  BEGIN
    SET @Correct = 1    
  END
  ELSE
  BEGIN
  	SET @Correct = 0
  	SET @UserScore = 0
  END    
          
  UPDATE  dbo.CGO_TriviaResult
  SET     ResultStatusID = 2,
          UserAnswerID = @UserAnswerID ,
          Correct = @Correct,          
          UserScore = @UserScore,
          Duration = @Duration,
          TimeEnd = GETDATE(),
          LastUpdateDate = GETDATE()
  WHERE   GameEnrollmentID = @EnrollID
          AND QuestionIndex = @QuestionIndex
  
  SELECT  @Total_Score = ISNULL(SUM(UserScore),0)
  FROM    dbo.CGO_TriviaResult
  WHERE   GameEnrollmentID = @EnrollID
          AND ResultStatusID = 2
          AND Correct = 1
  
  UPDATE  dbo.CGO_GameEnrollment
  SET     Score = @Total_Score ,
          LastUpdateDate = GETDATE()
  WHERE   EnrollmentID = @EnrollID
  
  SELECT @CorrectAnswerID
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO